//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// Copyright (C) 2007 Lars Brubaker
//                  larsbrubaker@gmail.com
//
// Permission to copy, use, modify, sell and distribute this software 
// is granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
using System;
using AGG;
using AGG.VertexSource;
using AGG.Transform;

namespace AGG.UI
{
    public class DragBarWidget : GUIWidget
    {
        bool m_MouseDownOnBar = false;
        Vector2D m_DownPosition;

        public DragBarWidget(rect_d InBounds)
        {
            Transform = Affine.NewTranslation(InBounds.Left, InBounds.Bottom);
            Bounds = new rect_d(0, 0, InBounds.Width, InBounds.Height);
        }

        protected bool MouseDownOnBar
        {
            get { return m_MouseDownOnBar; }
            set { m_MouseDownOnBar = value; }
        }

        public override void OnDraw(Graphics2D graphics2D)
        {
            RoundedRect roundRect = new RoundedRect(Bounds, 0);
            graphics2D.Render(roundRect, new RGBA_Bytes(0, 0, 0, 30));
            base.OnDraw(graphics2D);
        }

        override public void OnMouseDown(MouseEventArgs mouseEvent)
        {
            if (InRect(mouseEvent.X, mouseEvent.Y))
            {
                MouseDownOnBar = true;
                Vector2D mouseRelClient = new Vector2D(mouseEvent.X, mouseEvent.Y);
                m_DownPosition = mouseRelClient;
            }
            else
            {
                MouseDownOnBar = false;
            }
        }

        override public void OnMouseUp(MouseEventArgs mouseEvent)
        {
            MouseDownOnBar = false;
        }

        override public void OnMouseMove(MouseEventArgs mouseEvent)
        {
            if (MouseDownOnBar)
            {
                Vector2D mouseRelClient = new Vector2D(mouseEvent.X, mouseEvent.Y);

                Vector2D newLocation = Parrent.Location;
                newLocation.x += mouseRelClient.x - m_DownPosition.x;
                newLocation.y += mouseRelClient.y - m_DownPosition.y;
                Parrent.Invalidate();
                Parrent.Location = newLocation;
                Parrent.Invalidate();
            }
        }
    };

    public class WindowWidget : GUIWidget
    {
        public WindowWidget(rect_d InBounds)
        {
            Transform = Affine.NewTranslation(InBounds.Left, InBounds.Bottom);
            Bounds = new rect_d(0, 0, InBounds.Width, InBounds.Height);
            AddChild(new DragBarWidget(new rect_d(0, InBounds.Height - 20, InBounds.Width, InBounds.Height)));
        }

        public override void OnDraw(Graphics2D graphics2D)
        {
            RoundedRect roundRect = new RoundedRect(Bounds, 0);
            graphics2D.Render(roundRect, new RGBA_Bytes(0, 0, 0, 30));
            base.OnDraw(graphics2D);
        }

        override public void OnMouseDown(MouseEventArgs mouseEvent)
        {
            base.OnMouseDown(mouseEvent);
        }

        override public void OnMouseUp(MouseEventArgs mouseEvent)
        {
            base.OnMouseUp(mouseEvent);
        }

        override public void OnMouseMove(MouseEventArgs mouseEvent)
        {
            //if(Capture || 
            base.OnMouseMove(mouseEvent);
        }
    };
}
